package com.hk.servlet;

import com.hk.dao.AdminDao;
import com.hk.dao.DaoFactory;
import com.hk.dao.StudentDao;
import com.hk.dao.TearcherDao;
import com.hk.entity.Admin;
import com.hk.entity.Student;
import com.hk.entity.Tearcher;
import com.hk.utils.MD5;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.sql.SQLException;

@WebServlet
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        super.doPost(req, resp);
        String userName=req.getParameter("userName");
        String passWord=req.getParameter("passWord");
        String type=req.getParameter("type");
        HttpSession session=req.getSession();
        if(StringUtils.isNotBlank(type)){
            if("0".equals(type)){
//学生认证
                Student student= null;
                try {
                    student = DaoFactory.getInstance().getStudentDao().login(userName, MD5.encrypByMd5(MD5.encrypByMd5(passWord)));
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if(student!=null){
                    session.setAttribute("user",student);
                    session.setAttribute("type",type);
                    resp.sendRedirect("index.jsp");
                }else {
                    req.setAttribute("error","用户名或密码错误");
                    req.getRequestDispatcher("login.jsp").forward(req,resp);
                }
            }else if("1".equals(type)){
//老师认证
                Tearcher tearcher= null;
                try {
                    tearcher = DaoFactory.getInstance().getTearcher().login(userName, MD5.encrypByMd5(MD5.encrypByMd5(passWord)));
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if(tearcher!=null){
                    session.setAttribute("user",tearcher);
                    session.setAttribute("type",type);
                    resp.sendRedirect("index.jsp");
                }else {
                    req.setAttribute("error","用户名或密码错误");
                    req.getRequestDispatcher("login.jsp").forward(req,resp);
                }
            }else if("2".equals(type)){
//管理员认证
                Admin admin=new Admin();
                admin.setUserName(userName);
                admin.setPwd(MD5.encrypByMd5(MD5.encrypByMd5(passWord)));
                try {
                    Admin entity=DaoFactory.getInstance().getAdminDao().login(admin);
                    if(entity!=null){
                        //执行跳转
                        session.setAttribute("user",entity);
                        session.setAttribute("type",type);
                        resp.sendRedirect("index.jsp");
                    }else {
                        //用户名或密码错误！！
                        req.setAttribute("error","用户名或密码错误");
                        req.getRequestDispatcher("login.jsp").forward(req,resp);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }else {

        }
    }
}
